package com.dianganjiao.lib.util

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.graphics.Insets
import android.graphics.Point
import android.os.Build
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.util.DisplayMetrics
import android.view.*
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.annotation.RequiresApi
import com.dianganjiao.lib.AppContext


/**
 * create file time : 2021/3/2
 * create user : wxl
 * subscribe :
 */
class Screen {
    companion object {

        fun marginTopStatusBar(view: View) {
            marginTopView(getStatusBarHeight(view.context), view)
        }

        fun addStatusBarHeight(view: View) {
            val statusBarHeight = getStatusBarHeight(view.context)
            view.layoutParams.height = view.layoutParams.height + statusBarHeight
            view.requestLayout()
        }

        fun getStatusBarHeight(context: Context): Int {
            val resourceId: Int =
                context.resources.getIdentifier("status_bar_height", "dimen", "android")
            if (resourceId > 0) {
                return context.resources.getDimensionPixelSize(resourceId)
            } else {
                return Math.ceil(((if (VERSION.SDK_INT >= VERSION_CODES.M) 24 else 25) * context.resources.getDisplayMetrics().density).toDouble())
                    .toInt()
            }
        }

        private fun marginTopView(margin: Int, view: View) {
            val params = view.layoutParams
            if (params is RelativeLayout.LayoutParams) {
                params.topMargin += margin
            }
            if (params is LinearLayout.LayoutParams) {
                params.topMargin += margin
            }
            if (params is FrameLayout.LayoutParams) {
                params.topMargin += margin
            }
            view.requestLayout()
        }

        fun getHeight(context: Context): Int {
            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                val windowMetrics: WindowMetrics =
                    (context as Activity).getWindowManager().getCurrentWindowMetrics()
                val insets: Insets = windowMetrics.windowInsets
                    .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
                windowMetrics.bounds.height() - insets.top - insets.bottom
            } else {
                val displayMetrics = DisplayMetrics()
                (context as Activity).getWindowManager().getDefaultDisplay()
                    .getMetrics(displayMetrics)
                displayMetrics.heightPixels
            }
        }

        fun getWidth(context: Context): Int {
            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                val windowMetrics: WindowMetrics =
                    (context as Activity).getWindowManager().getCurrentWindowMetrics()
                val insets: Insets = windowMetrics.windowInsets
                    .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
                windowMetrics.bounds.width() - insets.left - insets.right
            } else {
                val displayMetrics = DisplayMetrics()
                (context as Activity).getWindowManager().getDefaultDisplay()
                    .getMetrics(displayMetrics)
                displayMetrics.widthPixels
            }
        }


        fun getWidth(): Int{
            return getWidth(AppContext.appContext.getCurrentActivity()!!)
        }

        fun getHeight():Int {
            return getHeight(AppContext.appContext.getCurrentActivity()!!)
        }
        /**
         * 按照屏幕宽度修改成均分尺寸
         */
        fun splitWidthHeightEquals(view: View, count: Int) {
            val width = getWidth(view.context)
            val s = width / count
            view.layoutParams = ViewGroup.LayoutParams(s, s)
        }

        /**
         * 按照屏幕宽度修改成均分尺寸
         */
        fun splitWidthHeightEquals(view: View, count: Int,offset:Int) {
            val width = getWidth(view.context) - offset
            val s = width / count
            view.layoutParams.height = s
            view.layoutParams.width = s
        }


        /**
         * 按照屏幕宽度修改成均分尺寸
         */
        fun splitWidthEquals(view: View, count: Int,offset:Int) {
            val width = getWidth(view.context) - offset
            val s = width / count
            view.layoutParams.width = s
        }


        /**
         * 获得虚拟按键高度
         */
        fun getVirtualBarHeight(context: Context): Int {
            var vh = 0
            val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val display = windowManager.getDefaultDisplay()
            val dm = DisplayMetrics()
            try {
                val c = Class.forName("android.view.Display")
                val method = c.getMethod("getRealMetrics", DisplayMetrics::class.java)
                method.invoke(display, dm)
                vh = dm.heightPixels - display.getHeight()
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return vh
        }


        @RequiresApi(VERSION_CODES.ICE_CREAM_SANDWICH)
        fun isNavigationBarShow(activity: Activity): Boolean {
            return if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
                val display: Display = activity.getWindowManager().getDefaultDisplay()
                val size = Point()
                val realSize = Point()
                display.getSize(size)
                display.getRealSize(realSize)
                realSize.y !== size.y
            } else {
                val menu = ViewConfiguration.get(activity).hasPermanentMenuKey()
                val back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)
                !(menu || back)
            }
        }

        @RequiresApi(VERSION_CODES.ICE_CREAM_SANDWICH)
        fun getNavigationBarHeight(activity: Activity): Int {
            if (!isNavigationBarShow(activity)) {
                return 0
            }
            val resources: Resources = activity.resources
            val resourceId: Int = resources.getIdentifier(
                "navigation_bar_height",
                "dimen", "android"
            )
            //获取NavigationBar的高度
            return resources.getDimensionPixelSize(resourceId)
        }

        /**
         * 添加虚拟按键的高度
         */
        @RequiresApi(VERSION_CODES.ICE_CREAM_SANDWICH)
        fun addVirtualBarHeight(view: View) {
            val statusBarHeight = getNavigationBarHeight(view.context as Activity)
            if(statusBarHeight > 0) {
                view.layoutParams.height = view.layoutParams.height + statusBarHeight
                view.requestLayout()
            }
        }


        /**
         * 添加偏移
         */
        @RequiresApi(VERSION_CODES.ICE_CREAM_SANDWICH)
        fun addMarginBottomVirtualBarHeight(view: View, bottomMargin:Int) {
            val statusBarHeight = getNavigationBarHeight(view.context as Activity)
            if(statusBarHeight > 0) {
                val params = view.layoutParams
                if (params is LinearLayout.LayoutParams) {
                    params.bottomMargin = statusBarHeight + bottomMargin
                }
                if (params is RelativeLayout.LayoutParams) {
                    params.bottomMargin = statusBarHeight + bottomMargin
                }
                if (params is ViewGroup.MarginLayoutParams) {
                    params.bottomMargin = statusBarHeight + bottomMargin
                }
            }
        }
    }
}